<?php defined('BASEPATH') || exit('No direct script access allowed');

/**
 * Building controller
 */
class Phone extends REST_Controller
{
    function __construct()
    {
        // Construct the parent class
        parent::__construct();
    }


    public function index_post()
    {
        $flag = false;
        $post = $this->post();
        $this->validPost($post);
        $this->load->model('phone/phone_model');
        $this->load->model('channel/channel_model');
        $this->load->model('buildings/buildings_model');
        $phoneField = ['dmp_building_code'=>'dmp 楼盘code', 'platform'=>'终端选项', 'number'=>'分机号', 'channel_code'=>'渠道code'];
        $data = [];
        foreach ($phoneField as $key=>$val) {
            if (isset($post[$key])) {
                $data[$key] = $post[$key];
            }
        }
        // 根据的dmp的code查询楼盘数据
        if(isset($data['dmp_building_code'])){
            $buildings = $this->buildings_model->find_by(['dmp_building_code'=>$post['dmp_building_code']]);
            if($buildings){
                $data['building_id'] = $buildings->id;
                $data['building_code'] = $buildings->code;
            }else{
                 $this->response(['status'=>false, 'message'=>'楼盘code不存在'], REST_Controller::HTTP_INTERNAL_SERVER_ERROR);
            }
        }else{
            $data['building_id'] = "";
            $data['building_code'] = "";
        }
        // 根据渠道code 查询渠道id
        if(isset($data['channel_code'])){
            $channel = $this->channel_model->find_by(['code'=>$post['channel_code']]);
            $data['channel_id']  =  $channel->id;
        }else{
            $data['channel_id'] = "";
        }     
        // 判断分机号是是否已存在
        $phoneExist = $this->phone_model->find_by(['number'=> $post['number']]);
        if($phoneExist){
            $res = $this->phone_model->update($phoneExist->id,$data);
        }else{
            $res = $this->phone_model->insert($data);
        }
        if ($res) $flag = true;
        $flag ? $this->response(['status'=>true, 'data'=>$post], REST_Controller::HTTP_CREATED) : $this->response(['status'=>false, 'message'=>'数据写入失败'], REST_Controller::HTTP_INTERNAL_SERVER_ERROR);
    }

    public function index_delete($number)
    {
        if(!isset($number) || empty($number)){
            $this->response(['status'=>false, 'message'=>'分机号 不能为空'], REST_Controller::HTTP_UNPROCESSABLE_ENTITY);
        }
        $this->load->model('phone/phone_model');
        $res = $this->phone_model->find_by(['number'=> $number, 'deleted'=>1]);
        if(!$res){
             $res = $this->phone_model->delete_where(['number'=> $number]);
        }
        if ($res) $flag = true;
        $flag ? $this->response(['status'=>true, 'data'=>$number], REST_Controller::HTTP_CREATED) : $this->response(['status'=>false, 'message'=>'数据删除失败'], REST_Controller::HTTP_INTERNAL_SERVER_ERROR);
    }

    private function validPost($data)
    {
        // $require = ['dmp_building_code'=>'dmp楼盘code', 'platform'=>'终端选项', 'number'=>'分机号', 'channel_code'=>'渠道code'];
        $require = ['number'=>'分机号'];
        foreach ($require as $key => $val) {
            if (!isset($data[$key]) || empty($data[$key])) {
                $this->response(['status'=>false, 'message'=>$val.' 不能为空'], REST_Controller::HTTP_UNPROCESSABLE_ENTITY);
                break;
            }
        }
    }
}